package com.example.esskill;

import com.example.esskill.bean.Goods;
import com.example.esskill.service.EsQueryDataService;
import com.example.esskill.service.impl.MyBusinessException;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.List;

/**
 * ClassName: ElasticsearchTest1 <br/>
 * Description: <br/>
 * date: 2024/2/4 16:12<br/>
 *
 * @author LXC<br />
 * @since JDK 1.8
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchTest1 {
    @Autowired
    EsQueryDataService esQueryDataService;

    /**
     * 单字段精确查询
     */
    @Test
    public void termQuery() {
        // 返回数据
        List<Goods> goodsList = null;
        try {
            goodsList = esQueryDataService.termQuery("goods", "title", "华为", Goods.class);

        } catch (Exception | MyBusinessException e) {
            log.error("单字段精确查询失败，错误信息：");
        }
        System.out.println("单字段精确查询结果：" + goodsList);
    }

    /**
     * 单字段多内容精确查询
     */
    @Test
    public void termsQuery() {
        // 返回数据
        List<Goods> goodsList = null;
        try {
            String[] args = {"华为", "OPPO", "TCL"};

            goodsList = esQueryDataService.termsQuery("goods", "title", args, Goods.class);

        } catch (Exception | MyBusinessException e) {
            log.error("单字段多内容精确查询失败，错误信息：");
        }
        System.out.println("单字段多内容精确查询结果：" + goodsList);
    }

    /**
     * 单字段匹配分页查询
     */
    @Test
    public void matchQuery() {
        // 返回数据
        List<Goods> goodsList = null;
        try {
           // List<String> orderList = UtilMisc.toList("-price","-saleNum");
            List<String> orderList = Arrays.asList("-price","-saleNum");

            goodsList = esQueryDataService.matchAllQuery("goods", Goods.class,0,3,orderList,"title", "华为");

        } catch (Exception e) {
            log.error("匹配查询失败，错误信息：");
        } catch (MyBusinessException e) {
            throw new RuntimeException(e);
        }
        System.out.println("匹配查询结果：" + goodsList);
    }


    /**
     * 单字段多内容精确查询
     */
    @Test
    public void matchPhraseQuery() {
        // 返回数据
        List<Goods> goodsList = null;
        try {

            goodsList = esQueryDataService.matchPhraseQuery("goods", Goods.class,"title", "华为");

        } catch (Exception | MyBusinessException e) {
            log.error("词语匹配查询失败，错误信息：");
        }

        System.out.println("词语匹配查询结果：" + goodsList);
    }

    /**
     * 内容在多字段中进行查询
     */
    @Test
    public void matchMultiQuery() {
        // 返回数据
        List<Goods> goodsList = null;
        try {
            String[] fields = {"title", "categoryName"};

            goodsList = esQueryDataService.matchMultiQuery("goods", Goods.class,fields,"手机");

        } catch (Exception | MyBusinessException e) {
            log.error("内容在多字段中进行查询失败，错误信息：");
        }

        System.out.println("内容在多字段中进行查询结果：" + goodsList);
    }

    /**
     * 通配符查询
     *
     * 查询所有以 “三” 结尾的商品信息
     */
    @Test
    public void wildcardQuery() {
        // 返回数据
        List<Goods> goodsList = null;
        try {

            goodsList = esQueryDataService.wildcardQuery("goods", Goods.class,"title","*三");

        } catch (Exception | MyBusinessException e) {
            log.error("通配符查询查询失败，错误信息：");
        }

        System.out.println("通配符查询结果：" + goodsList);
    }

    /**
     * 模糊查询
     *
     * 模糊查询所有以 “三” 结尾的商品信息
     */
    @Test
    public void fuzzyQuery() {
        // 返回数据
        List<Goods> goodsList = null;
        try {

            goodsList = esQueryDataService.fuzzyQuery("goods", Goods.class,"title","三");

        } catch (Exception | MyBusinessException e) {
            log.error("模糊查询失败，错误信息：");
        }

        System.out.println("模糊查询结果：" + goodsList);
    }


    @Test
    public void boolQuery() {
        // 返回数据
        List<Goods> goodsList = null;
        try {
            goodsList = esQueryDataService.boolQuery("goods", Goods.class);

        } catch (Exception | MyBusinessException e) {
            log.error("布尔查询失败，错误信息：");
        }

        System.out.println("布尔查询结果：" + goodsList);
    }

    /**
     * Metric 指标聚合分析
     */
    @Test
    public void metricQuery() throws MyBusinessException, Exception {
        esQueryDataService.metricQuery("goods");
    }


    /**
     * Bucket 分桶聚合分析
     */
    @Test
    public void bucketQuery() throws MyBusinessException {
        esQueryDataService.bucketQuery("goods","brandName.keyword","brandName");
    }

    /**
     * 子聚合聚合查询
     */
    @Test
    public void subBucketQuery() throws MyBusinessException {
        esQueryDataService.subBucketQuery("goods","brandName.keyword","brandNameName","price","avgPrice");
    }

    /**
     * 综合聚合查询
     */
    @Test
    public void subSubAgg() throws MyBusinessException {
        esQueryDataService.subSubAgg("goods");
    }
}
